package com.ruoyi.web.controller.basic;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.URLUtil;
import com.alibaba.excel.EasyExcel;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basic.domain.JyCourseCategory;
import com.ruoyi.basic.service.IJyCourseCategoryService;

/**
 * 课程分类Controller
 *
 * @author aulbrother
 * @date 2024-04-05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basic/category")
public class CourseCategoryController extends BaseController {
    private final IJyCourseCategoryService jyCourseCategoryService;

    /**
     * 查询课程分类列表
     */
    @PreAuthorize("@ss.hasPermi('basic:category:list')")
    @GetMapping("/list")
    public AjaxResult list(JyCourseCategory jyCourseCategory) {
        List<JyCourseCategory> list = jyCourseCategoryService.selectJyCourseCategoryList(jyCourseCategory);
        return success(list);
    }

    /**
     * 导出课程分类列表
     */
    @PreAuthorize("@ss.hasPermi('basic:category:export')")
    @Log(title = "课程分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JyCourseCategory jyCourseCategory) throws IOException {
        List<JyCourseCategory> list = jyCourseCategoryService.selectJyCourseCategoryList(jyCourseCategory);

        // Step.3 调用AutoPoi导出Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String sheetName = "课程分类数据";
        String fileName = URLUtil.encode(sheetName).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), JyCourseCategory.class).sheet(sheetName).doWrite(list);
    }

    /**
     * 获取课程分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jyCourseCategoryService.selectJyCourseCategoryById(id));
    }

    /**
     * 新增课程分类
     */
    @PreAuthorize("@ss.hasPermi('basic:category:add')")
    @Log(title = "课程分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JyCourseCategory jyCourseCategory) {
        return toAjax(jyCourseCategoryService.insertJyCourseCategory(jyCourseCategory));
    }

    /**
     * 修改课程分类
     */
    @PreAuthorize("@ss.hasPermi('basic:category:edit')")
    @Log(title = "课程分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JyCourseCategory jyCourseCategory) {
        return toAjax(jyCourseCategoryService.updateJyCourseCategory(jyCourseCategory));
    }

    /**
     * 删除课程分类
     */
    @PreAuthorize("@ss.hasPermi('basic:category:remove')")
    @Log(title = "课程分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jyCourseCategoryService.deleteJyCourseCategoryByIds(ids));
    }
}
